package com.lemner.ke.activity;

import android.Manifest;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.laojiang.imagepickers.ImagePicker;
import com.laojiang.imagepickers.data.ImagePickType;
import com.laojiang.imagepickers.data.MediaDataBean;
import com.lemner.ke.R;
import com.lemner.ke.base.BaseActivity;
import com.lemner.ke.base.BaseListener;
import com.lemner.ke.bean.SuccessBean;
import com.lemner.ke.model.mine.FeedbackModel;
import com.lemner.ke.util.MyImagePicker;
import com.lemner.ke.util.PhotoAlbumUtil;
import com.lemner.ke.util.SpUtils;
import com.lemner.ke.view.MyPopWindow;

import java.io.File;
import java.util.List;

import kr.co.namee.permissiongen.PermissionFail;
import kr.co.namee.permissiongen.PermissionGen;
import kr.co.namee.permissiongen.PermissionSuccess;

/**

* 作者：张恺

* 时间：2018/9/25

* 类描述：意见反馈页面

*/

public class FeedbackActivity extends BaseActivity {
    private Button btn_commit;
    private ImageView iv_img;
    private TextView tv_addImage;
    private MyPopWindow popup_img;
    private EditText et_content;

    @Override
    public int getLayoutFile() {
        return R.layout.activity_feedback;
    }

    @Override
    public void initView() {
        btn_commit=findViewById(R.id.btn_feedback_commit);
        iv_img=findViewById(R.id.iv_feedback_img);
        et_content=findViewById(R.id.et_feedback_content);
        tv_addImage=findViewById(R.id.tv_feedback_addImage);
    }

    @Override
    public void initEvent() {
        iv_img.setOnClickListener(this);
        btn_commit.setOnClickListener(this);
    }

    @Override
    public void initData() {

    }

    @Override
    protected void initSetting() {
        topBar.setTitle("意见反馈");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_feedback_commit:
                String content = "";
                content = et_content.getText().toString();
                // feed_back
                new FeedbackModel().feedback(SpUtils.getString(this, SpUtils.USERIDS), content, new BaseListener<SuccessBean>() {
                    @Override
                    public void onResponse(SuccessBean bean) {
                        showToast("反馈成功");
                        finish();
                    }

                    @Override
                    public void onFail(String msg) {
                        showToast("反馈成功");
                        finish();
                    }
                });
                break;
            case R.id.iv_feedback_img:
                popup_img = new MyPopWindow(this);
                popup_img.addButton("相册", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popup_img.dismiss();
                        new MyImagePicker(FeedbackActivity.this, ImagePickType.SINGLE, false);
                    }
                });
                popup_img.addButton("相机", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popup_img.dismiss();
                        PermissionGen.with(FeedbackActivity.this)
                                .addRequestCode(105)
                                .permissions(
                                        Manifest.permission.CAMERA,
                                        Manifest.permission.WRITE_EXTERNAL_STORAGE)
                                .request();
                    }
                });
                popup_img.showAtLocation();
                break;
        }
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        PermissionGen.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
    }

    @PermissionSuccess(requestCode = 105)
    public void doSomething() {
        new MyImagePicker(this, ImagePickType.ONLY_CAMERA, false);
    }

    @PermissionFail(requestCode = 105)
    public void doFailSomething() {
        showToast("相机权限打开失败！");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == MyImagePicker.RESULT_CODE && data != null) {
            super.onActivityResult(requestCode, resultCode, data);
            List<MediaDataBean> resultList = data.getParcelableArrayListExtra(ImagePicker.INTENT_RESULT_DATA);
            File picture = new File(resultList.get(0).getMediaPath());
            if (picture.exists()) {
                Bitmap bitmap = PhotoAlbumUtil.getBitmapFromUri(this, Uri.fromFile(picture));
                bitmap = PhotoAlbumUtil.zoomImage(this, bitmap);
                iv_img.setImageBitmap(bitmap);
                tv_addImage.setVisibility(View.GONE);

                if (picture.exists()) {
                    bitmap = PhotoAlbumUtil.getBitmapFromUri(this, Uri.fromFile(picture));
                    bitmap = PhotoAlbumUtil.zoomImage(this, bitmap);
                }
            }
        }
    }
}
